package com.ns.school.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, String value) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key, value);
        return true;
    }

    /**
     * 写入缓存设置时效时间(秒)
     *
     * @param key        缓存key
     * @param value      缓存数据
     * @param expireTime (秒)
     * @return
     */
    public boolean set(final String key, String value, long expireTime) {
        set(key, value, expireTime, TimeUnit.SECONDS);
        return true;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key        缓存key
     * @param value      缓存数据
     * @param expireTime 缓存时间
     * @param timeUnit   缓存时间单位
     * @return
     */
    public boolean set(final String key, String value, long expireTime, TimeUnit timeUnit) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key, value);
        redisTemplate.expire(key, expireTime, timeUnit);
        return true;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public String get(final String key) {
        String result;
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setObject(final String key, Object value) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key, JsonUtil.toStr(value));
        return true;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key        缓存key
     * @param value      缓存数据
     * @param expireTime (秒)
     * @return
     */
    public boolean setObject(final String key, Object value, long expireTime) {
        setObject(key, value, expireTime, TimeUnit.SECONDS);
        return true;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key        缓存key
     * @param value      缓存数据
     * @param expireTime 缓存时间
     * @param timeUnit   缓存时间单位
     * @return
     */
    public boolean setObject(final String key, Object value, long expireTime, TimeUnit timeUnit) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key, JsonUtil.toStr(value));
        redisTemplate.expire(key, expireTime, timeUnit);
        return true;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public <T> T getObject(final String key, Class<T> clazz) {
        String result;
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        if (StringUtils.isNotEmpty(result)) {
            return JsonUtil.toJavaObject(result, clazz);
        }
        return null;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Integer getInteger(final String key) {
        return getObject(key, Integer.class);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Long getLong(final String key) {
        return getObject(key, Long.class);
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setList(final String key, Object value) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key, JsonUtil.toStr(value));
        return true;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key        缓存key
     * @param value      缓存数据
     * @param expireTime (秒)
     * @return
     */
    public boolean setList(final String key, List<?> value, long expireTime) {
        setList(key, value, expireTime, TimeUnit.SECONDS);
        return true;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key        缓存key
     * @param value      缓存数据
     * @param expireTime 缓存时间
     * @param timeUnit   缓存时间单位
     * @return
     */
    public boolean setList(final String key, List<?> value, long expireTime, TimeUnit timeUnit) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key, JsonUtil.toStr(value));
        redisTemplate.expire(key, expireTime, timeUnit);
        return true;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public <T> List<T> getList(final String key, Class<T> clazz) {
        String result;
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        if (StringUtils.isNotEmpty(result)) {
            return JsonUtil.toList(result, clazz);
        }
        return null;
    }

    /**
     * 自增+1[redis中的数据从1.2.3.4...开始]
     *
     * @param key
     */
    public long incr(final String key) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        return operations.increment(key);
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys != null && keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        Boolean result = redisTemplate.hasKey(key);
        return result != null && result;
    }

    /**
     * 哈希 添加
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hmSet(final String key, String hashKey, String value) {
        HashOperations<String, String, String> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }

    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */
    public Object hmGet(final String key, String hashKey) {
        HashOperations<String, String, String> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void lPush(final String k, String v) {
        ListOperations<String, String> list = redisTemplate.opsForList();
        list.rightPush(k, v);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param start
     * @param end
     * @return
     */
    public List<String> lRange(final String k, long start, long end) {
        ListOperations<String, String> list = redisTemplate.opsForList();
        return list.range(k, start, end);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void add(final String key, String value) {
        SetOperations<String, String> set = redisTemplate.opsForSet();
        set.add(key, value);
    }

    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<String> setMembers(final String key) {
        SetOperations<String, String> set = redisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param score
     */
    public void zAdd(final String key, String value, double score) {
        ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
        zset.add(key, value, score);
    }

    /**
     * 有序集合获取
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Set<String> rangeByScore(final String key, double min, double max) {
        ZSetOperations<String, String> set = redisTemplate.opsForZSet();
        return set.rangeByScore(key, min, max);
    }
}
